package com.baijiaoxi.common.model.query;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Data

public class DynamicCondition<T> implements Serializable {
    @ApiModelProperty("与同级的逻辑关系：and、or")
    protected String logicMethod;
    protected String propertyName;
    @ApiModelProperty(value = "查询类型" ,
            allowableValues = "in，not-in，include，exclude，equals，not-blank，blank，greater，greater-equals，less，less-equals" ,
            notes = "in在范围中，not-in不在范围中，include包含，exclude不包含，equals等于，not-blank不为空，blank为空，greater大于，greater-equals大于等于，less小于，less-equals小于等于")
    protected String method;
    @ApiModelProperty("对比值")
    protected String value;
    @ApiModelProperty("子级条件列表")
    protected List<T> children = new ArrayList<>();//子表达式

    public DynamicCondition() {
    }

    public DynamicCondition(String propertyName, String method, String value) {
        this.propertyName = propertyName;
        this.method = method;
        this.value = value;
    }

    public DynamicCondition(String propertyName, String method) {
        this.propertyName = propertyName;
        this.method = method;
    }


    public DynamicCondition setPropertyName(String propertyName) {
        this.propertyName = propertyName;
        return this;
    }

    public DynamicCondition setMethod(String method) {
        this.method = method;
        return this;
    }

    public DynamicCondition setValue(String value) {
        this.value = value;
        return this;
    }

    public DynamicCondition setLogicMethod(String logicMethod) {
        this.logicMethod = logicMethod;
        return this;
    }

    public DynamicCondition addChild(T child) {
        this.children.add(child);
        return this;
    }

    public DynamicCondition addChild(List<T> children) {
        this.children.addAll(children);
        return this;
    }


    @Override
    public String toString() {
        return "DynamicCondition:{\"propertyName\"=\"" + propertyName + "\",\"logicMethod\"=\"" + logicMethod + "\"," +
                "\"method\"=\"" + method + "\",\"value\"=\"" + value + "\"," +
                "\"children\"=[" +
                children != null && !children.isEmpty() ? String.join("," , children.stream().map(item -> item.toString()).collect(Collectors.toList()))
                : ""
                + "]}"
                ;

    }
}

